package io.github.uicdb.badskill.codec;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.gson.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraftforge.registries.ForgeRegistries;

import java.lang.reflect.Type;
import java.util.UUID;
import java.util.function.Consumer;

public class AttributeMultimapCodec implements JsonDeserializer<Multimap<Attribute, AttributeModifier>>, JsonSerializer<Multimap<Attribute, AttributeModifier>> {
    public static final AttributeMultimapCodec INSTANCE=new AttributeMultimapCodec();
    @Override
    public Multimap<Attribute, AttributeModifier> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        HashMultimap<Attribute,AttributeModifier> result=HashMultimap.create();
        for (JsonElement attrE : json.getAsJsonArray()) {
            var attrObj=attrE.getAsJsonObject();
            Attribute attribute=ForgeRegistries.ATTRIBUTES.getValue(new ResourceLocation(attrObj.get("attribute").getAsString()));
            attrObj.getAsJsonArray("modifiers")
                    .forEach(element -> {
                        JsonObject innerObj = element.getAsJsonObject();
                        UUID uuid = UUID.fromString(innerObj.get("uuid").getAsString());
                        String name = innerObj.get("name").getAsString();
                        double amount = innerObj.get("amount").getAsDouble();
                        AttributeModifier.Operation operation = AttributeModifier.Operation.valueOf(innerObj.get("operation").getAsString());
                        result.put(attribute, new AttributeModifier(uuid, name, amount, operation));
                    });
        }
        return result;
    }

    @Override
    public JsonElement serialize(Multimap<Attribute, AttributeModifier> src, Type typeOfSrc, JsonSerializationContext context) {
        JsonArray root=new JsonArray();
        src.keySet().forEach(attribute -> {
            JsonObject attrObj = new JsonObject();
            JsonArray modArray = new JsonArray();
            src.get(attribute)
                    .forEach(modifier -> {
                        JsonObject modObj = new JsonObject();
                        modObj.addProperty("uuid", modifier.getId().toString());
                        modObj.addProperty("name", modifier.getName());
                        modObj.addProperty("amount", modifier.getAmount());
                        modObj.addProperty("operation", modifier.getOperation().name());
                        modArray.add(modObj);
                    });
            attrObj.addProperty("attribute", ForgeRegistries.ATTRIBUTES.getKey(attribute).toString());
            attrObj.add("modifiers", modArray);
            root.add(attrObj);
        });
        return root;
    }
}
